<?php
class PostUser extends CActiveRecord{
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'tbl_post_user';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function getRowByUserID($user_id){        
        $cache=false;
        if($cache == false){ 
            $sql = "SELECT * FROM tbl_post_user INNER JOIN tbl_member ON tbl_post_user.group_id = tbl_member.group_id WHERE user_id='".$user_id."'";            
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll();            
        }
        return $row;
    }    

    public function getRowById($id){
        $sql = "SELECT * FROM tbl_post_user WHERE grade_id=".$id;                      
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
        return $row;
    } 
    
    public function getPostsByReceiverID($user_id){
        //$sql = "SELECT tbl_post_user.*,tbl_user.user_fullname AS `sender_name` FROM tbl_post_user INNER JOIN tbl_user ON tbl_post_user.pUser_sender_id = tbl_user.user_id WHERE pUser_receiver_id=".$user_id ." ORDER BY pUser_create_date desc";                              
        $sql = "SELECT tbl_post_user.*,tbl_user.user_fullname AS `sender_name` FROM tbl_post_user INNER JOIN tbl_user ON tbl_post_user.pUser_sender_id = tbl_user.user_id WHERE pUser_receiver_id=".$user_id ." OR pUser_sender_id=".$user_id ." ORDER BY pUser_create_date desc";                              
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row;
    } 
    
    public function getRowByGroupCode($group_code){
        $sql = "SELECT * FROM tbl_post_user WHERE group_code='".$group_code."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
        return $row;
    }           

    public function getAllRows(){

        $sql = "SELECT * FROM tbl_post_user";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();


        return $rows;
    }    

    public function insertObject($array_input){
        $sql='';
        foreach($array_input as $key=>$value)
        {
            $sql.=$key."='".$value."',"; 
        }
        $sql='INSERT INTO tbl_post_user SET '.$sql;
        $sql=rtrim($sql,',');
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $command->execute();
        $record_id=Yii::app()->db->getLastInsertID();  
        return $record_id;
    }

    public function updateObject($array_input,$key_id,$key_value){
        $sql='';
        foreach($array_input as $key=>$value)
        {
            $sql.=$key."='".$value."',"; 
        }
        $sql=rtrim($sql,',');
        if($sql!='')
        {
            $sql='UPDATE tbl_post_user SET '.$sql.' WHERE '.$key_id.'='.$key_value;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $a=$command->execute();
            return $a;
        }
        return -1;
    }    

    public function deleteObject($array_input){
        $sql=' 1 ';
        foreach($array_input as $key=>$value)
        {
            $sql.=" AND ".$key."='".$value."'"; 
        }
        $sql=rtrim($sql,',');
        if($sql!='')
        {
            $sql='delete from tbl_post_user where '.$sql.'';
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $a=$command->execute();
            if($a) return 1;
            else return 0;
        }
        else return 0;

    }

}
